//
//  TradeOrderViewModel.swift
//  Exchange
//
//  Created by rui on 2018/4/25.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class WaitOrderViewModel: BaseViewModel {
    
    var isNoMoreData = false
    
    var page: Int = 1
    let size: Int = 15
    
    ///删除的id
    var id: Int = 0
    
    var dataArray = [WaitOrderModel]()
    
    var indexPath: IndexPath = IndexPath()
    
    private lazy var waitOrderListApi: WaitOrderListRequest = {
        let api = WaitOrderListRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    private lazy var waitOrderDeleteApi: WaitOrderDeleteRequest = {
        let api = WaitOrderDeleteRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    
    
    func deleteWaitOrder(id: Int,indexPath: IndexPath) {
        self.id = id
        self.indexPath = indexPath
        self.waitOrderDeleteApi.loadData()
    }
    
    func loadNewData() {
        self.page = 1
        self.waitOrderListApi.loadData()
    }
    
    func loadMoreData() {
        self.page += 1
        self.waitOrderListApi.loadData()
    }
    
}

extension WaitOrderViewModel: RequestCallBack,RequestDataReformer,RequestParamSource {
    
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
        if let _ = request as? WaitOrderListRequest {
            var flag = false
            if let realyData = data?["data"] as? [String:Any] {
                if let list = realyData["list"] as? [[String:Any]] {
                    if let jsonData = list.jsonData {
                        do{
                            let list = try JSONDecoder().decode(Array<WaitOrderModel>.self, from: jsonData)
                            if self.page == 1 {
                                self.dataArray.removeAll()
                            }
                            if let hasNext =  realyData["hasNextPage"] as? Bool {
                                self.isNoMoreData = !hasNext
                            }
                            self.dataArray.append(contentsOf: list)
                            flag = true
                        }catch {
                            flag = false
                            dPrint(error)
                        }
                    }
                }
            }
            if !flag {
                self.page -= 1
            }
            self.response?(ResponseState.trade_order_wait_list,flag, nil)
        }else if let _ = request as? WaitOrderDeleteRequest {
            if indexPath.row < self.dataArray.count {
                self.dataArray.remove(at: indexPath.row)
            }
            self.response?(ResponseState.trade_order_wait_delete,true, nil)
        }
    }
    
    func requestDidSucess(_ request: BaseRequest) {
        if let _ = request as? WaitOrderListRequest {
            self.isNoMoreData = false
        }
    }
    
    func requestDidFail(_ request: BaseRequest) {
        if let _ = request as? WaitOrderListRequest {
            self.isNoMoreData = false
            self.page -= 1
            self.response?(ResponseState.trade_order_wait_list,false, request.errorMsg)
        }else if let _ = request as? WaitOrderDeleteRequest {
            self.response?(ResponseState.trade_order_wait_delete,false, nil)
        }
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        var pramas: [String:Any] = [:]
        if let _ = request as? WaitOrderListRequest {
            pramas.updateValue(size, forKey: "size")
            pramas.updateValue(page, forKey: "page")
        }else if let _ = request as? WaitOrderDeleteRequest {
            pramas.updateValue(id, forKey: kID)
        }
        return pramas
    }
    
}
